package com.lcz.authority.admin.mapper;

import com.lcz.authority.common.core.mybatis.BaseMapperX;
import com.lcz.authority.common.core.mybatis.LambdaQueryWrapperX;
import com.lcz.authority.common.core.mybatis.MPJLambdaWrapperX;
import com.lcz.authority.common.core.page.PageResult;
import com.lcz.authority.common.domain.entity.SysRoleEntity;
import com.lcz.authority.common.domain.entity.SysUserEntity;
import com.lcz.authority.common.domain.entity.SysUserRoleEntity;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Set;

/**
 * @author lcz
 * @desc 角色映射器
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */

public interface SysRoleMapper extends BaseMapperX<SysRoleEntity> {
    default PageResult<SysRoleEntity> selectPage(SysRoleEntity role) {

        return selectPage(new LambdaQueryWrapperX<SysRoleEntity>()
                .likeIfPresent(SysRoleEntity::getRoleName, role.getRoleName())
                .likeIfPresent(SysRoleEntity::getRoleKey, role.getRoleKey())
                .eqIfPresent(SysRoleEntity::getStatus, role.getStatus())
        );
    }

    /**
     * 根据用户ID查询角色
     *
     * @param userId 用户ID
     * @return 角色列表
     */
    default List<SysRoleEntity> selectRolePermissionByUserId(String userId) {
        MPJLambdaWrapperX<SysRoleEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysRoleEntity.class);
        wrapperX.leftJoin(SysUserRoleEntity.class, SysUserRoleEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserRoleEntity::getUserId);
        wrapperX.eq(SysRoleEntity::getDelFlag, "0");
        wrapperX.eq(SysUserEntity::getUserId, userId);
        return selectList(wrapperX);
    }

    /**
     * 通过角色ID查询角色
     *
     * @param roleId 角色ID
     * @return 角色对象信息
     */
    default SysRoleEntity selectRoleById(String roleId) {
        //       <include refid="selectRoleVo"/>
        //        where r.role_id = #{roleId}
        MPJLambdaWrapperX<SysRoleEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysRoleEntity.class);
        wrapperX.leftJoin(SysUserRoleEntity.class, SysUserRoleEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserRoleEntity::getUserId);
        wrapperX.eq(SysRoleEntity::getRoleId, roleId);
        return selectOne(wrapperX);
    }

    /**
     * 根据条件分页查询角色数据
     *
     * @param role 角色信息
     * @return 角色数据集合信息
     */
    default List<SysRoleEntity> selectRoleList(SysRoleEntity role) {
        //<include refid="selectRoleVo"/>
        //        where r.del_flag = '0'
        //        <if test="roleId != null and roleId != 0">
        //            AND r.role_id = #{roleId}
        //        </if>
        //        <if test="roleName != null and roleName != ''">
        //            AND r.role_name like concat('%', #{roleName}, '%')
        //        </if>
        //        <if test="status != null and status != ''">
        //            AND r.status = #{status}
        //        </if>
        //        <if test="roleKey != null and roleKey != ''">
        //            AND r.role_key like concat('%', #{roleKey}, '%')
        //        </if>
        //        <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
        //            and date_format(r.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
        //        </if>
        //        <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
        //            and date_format(r.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
        //        </if>
        //        order by r.role_sort
        MPJLambdaWrapperX<SysRoleEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysRoleEntity.class);
        wrapperX.leftJoin(SysUserRoleEntity.class, SysUserRoleEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserRoleEntity::getUserId);
        wrapperX.eq(SysRoleEntity::getDelFlag, "0");
        wrapperX.eqIfPresent(SysRoleEntity::getRoleId, role.getRoleId());
        wrapperX.likeIfPresent(SysRoleEntity::getRoleName, role.getRoleName());
        wrapperX.eqIfPresent(SysRoleEntity::getStatus, role.getStatus());
        wrapperX.likeIfPresent(SysRoleEntity::getRoleKey, role.getRoleKey());
        wrapperX.betweenIfPresent(SysRoleEntity::getCreateTime, role.getParams().get("beginTime"), role.getParams().get("endTime"));
        wrapperX.orderByAsc(SysRoleEntity::getRoleSort);
        return selectList(wrapperX);
    }

    /**
     * 根据角色权限字符串查询角色数据
     *
     * @param RoleKeys
     * @return
     */
    default List<SysRoleEntity> selectRoleListByKey(@Param("RoleKeys") Set<String> RoleKeys) {
        //        <include refid="selectRoleVo"/>
        //        where r.del_flag = '0'
        //        and r.role_key in
        //        <foreach item="roleKey" collection="RoleKeys" open="(" separator="," close=")">
        //            #{roleKey}
        //        </foreach>
        MPJLambdaWrapperX<SysRoleEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysRoleEntity.class);
        wrapperX.leftJoin(SysUserRoleEntity.class, SysUserRoleEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserRoleEntity::getUserId);
        wrapperX.eq(SysRoleEntity::getDelFlag, "0");
        wrapperX.inIfPresent(SysRoleEntity::getRoleKey, RoleKeys);
        return selectList(wrapperX);
    }

    /**
     * 校验角色名称是否唯一
     *
     * @param roleName 角色名称
     * @return 角色信息
     */
    default SysRoleEntity checkRoleNameUnique(String roleName) {
        //        <include refid="selectRoleVo"/>
        //        where r.role_name=#{roleName} and r.del_flag = '0' limit 1
        MPJLambdaWrapperX<SysRoleEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysRoleEntity.class);
        wrapperX.leftJoin(SysUserRoleEntity.class, SysUserRoleEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserRoleEntity::getUserId);
        wrapperX.eq(SysRoleEntity::getDelFlag, "0");
        wrapperX.eq(SysRoleEntity::getRoleName, roleName);
        return selectOne(wrapperX);
    }

    /**
     * 校验角色权限是否唯一
     *
     * @param roleKey 角色权限
     * @return 角色信息
     */
    default SysRoleEntity checkRoleKeyUnique(String roleKey) {
        //  <include refid="selectRoleVo"/>
        //        where r.role_key=#{roleKey} and r.del_flag = '0' limit 1
        MPJLambdaWrapperX<SysRoleEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysRoleEntity.class);
        wrapperX.leftJoin(SysUserRoleEntity.class, SysUserRoleEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserRoleEntity::getUserId);
        wrapperX.eq(SysRoleEntity::getDelFlag, "0");
        wrapperX.eq(SysRoleEntity::getRoleKey, roleKey);
        return selectOne(wrapperX);
    }

    /**
     * 根据用户ID查询角色
     *
     * @param userName 用户名
     * @return 角色列表
     */
    default List<SysRoleEntity> selectRolesByUserName(String userName) {
        MPJLambdaWrapperX<SysRoleEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysRoleEntity.class);
        wrapperX.leftJoin(SysUserRoleEntity.class, SysUserRoleEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserRoleEntity::getUserId);
        wrapperX.eq(SysRoleEntity::getDelFlag, "0");
        wrapperX.eq(SysUserEntity::getUserName, userName);
        return selectList(wrapperX);
    }

}
